package com.vectz.autogen.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.vectz.autogen.model.Entity;
import com.vectz.autogen.model.EntityOverview;
import com.vectz.autogen.util.Constant;
import com.vectz.autogen.util.Util;
/**
 * @author ai-t.vn
 *
 */
public class ExcelAction {
	private FileInputStream fis = null;
	private HSSFWorkbook wb = null;
	private HSSFSheet sheet = null;

	public ExcelAction(HSSFWorkbook wb, HSSFSheet sheet) {
		this.wb = wb;
		this.sheet=sheet;
	}
	/**
	 * @return table 
	 */
	public EntityOverview getOverviewInfo() {
		EntityOverview table = new EntityOverview();
		try {
			table.setName(Util.getCellValue(sheet, wb,Util.getParamConfig("ENTITY_NAME_COL")));
			table.setTableName(Util.getCellValue(sheet, wb,Util.getParamConfig("TABLE_NAME_COL")));
			table.setScreenId(Util.getCellValue(sheet, wb,Util.getParamConfig("SCREEN_ID_COL")));
			table.setScreenName(Util.getCellValue(sheet, wb,Util.getParamConfig("SCREEN_NAME_COL")));
			table.setPackageName(Util.getCellValue(sheet, wb,Util.getParamConfig("PACKAGE_NAME_COL")));
			table.setDestPath(Util.getCellValue(sheet, wb,Util.getParamConfig("DEST_PATH_COL")));
			table.setDestScreenPath(Util.getCellValue(sheet, wb,Util.getParamConfig("DEST_SCREEN_PATH_COL")));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return table;
		
	}
	
	/*
	 * get list data field define
	 */
	public ArrayList<Entity> getListDataTable() {
		ArrayList<Entity> listData = new ArrayList<Entity>();
		Entity data = null;
		try {
			int beginRow = Integer.parseInt(Util.getParamConfig("BEGIN_DATA_ROW"));
			int endRow = sheet.getLastRowNum();
			if (beginRow != 0 && endRow != 0) {
				// loop after header row = beginRow - 1 because first row is 0
				for (int i = beginRow -1; i < endRow; i++) {
					
					HSSFRow hssRow = sheet.getRow(i);
					// get No cell
					HSSFCell numberCell = hssRow.getCell(Util.convertColumnNameToNumber(Util.getParamConfig("NUMBER_COL")));
					// if No cell contain null then break loop
					if(numberCell==null || Util.isEmpty(String.valueOf(numberCell.getNumericCellValue())))
						break;
					try {
						data = new Entity();
						data = Util.getDataTable(sheet, wb, hssRow);
						listData.add(data);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listData;

	}
}
